<?php
/**
 * 自定义表单
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: DiyFormModel.class.php v1.0 2014-06-12 17:27:03 $
 */

namespace Admin\Service;
class DiyFormService extends CommonService{
    
    /**
     * 数据入库
     * @param type $data
     * @return type
     */
    public function add($data) {
        $DiyModel = $this->getD();
        if(false === ($diyforms = $DiyModel->create($data))){
            return $this->errorResultReturn($DiyModel->getError());
        }
        $table = C("DB_PREFIX") . $data['formtable'];
        $mysql_version = $DiyModel->GetVersion();
        $sql = "CREATE TABLE IF NOT EXISTS  `$table`(
        `id` int(10) unsigned NOT NULL auto_increment,
        `ifcheck` tinyint(1) NOT NULL default '0',
        `subtime` int(10) NOT NULL default '0',
        ";
        if($mysql_version < 4.1)
        {
            $sql .= " PRIMARY KEY  (`id`)\r\n) TYPE=MyISAM; ";
        }
        else
        {
            $sql .= " PRIMARY KEY  (`id`)\r\n) ENGINE=MyISAM DEFAULT CHARSET=utf8; ";
        }
        
        if(false === $DiyModel->execute($sql)){
            return $this->errorResultReturn("自定义表单创建失败！");
        }
        if(false === $DiyModel->add($diyforms)){
            $DiyModel->dropTable($table);
            return $this->errorResultReturn("自定义表单创建失败！");
        }
        
        return $this->resultReturn(TRUE);
    }
    
    /**
     * 更新数据
     * @param type $data
     * @param type $add 
     * @return type
     */
    public function update($data, $add = '') {
        $DiyModel = $this->getD();
        if($add == 'field'){//更新字段信息
            $diyforms = $data;
        }else{
            if(false === ($diyforms = $DiyModel->create($data))){
                return $this->errorResultReturn($DiyModel->getError());
            }
        }
        if(false === $DiyModel->save($diyforms)){
            return $this->errorResultReturn("自定义表单创建失败！");
        }
        
        return $this->resultReturn(TRUE);
    }
    
    /**
     * 删除
     * @param type $id
     * @return type
     */
    public function delete($id) {
        return $this->getM()->where(array("id" => $id))->delete();
    }
    /**
     * 获取一行数据
     * @param type $where
     * @param type $order
     * @return type
     */
    public function getRow($where, $orderby) {
        return $this->getD()->where($where)->order($orderby)->limit(0,1)->find();
    }
    
    /**
     * 根据ID获取数据
     * @param type $id
     * @return type
     */
    public function getById($id) {
        return $this->getD()->getById($id);
    }
    
    /**
     * 获取自定义表单数据
     * @param int $id   自定义表单ID
     * @return array
     */
    public function getDiyList($diyinfo) {
        if(!isset($diyinfo['formtable'])){
            return NULL;
        }
        $M = M($diyinfo['formtable']);
        $count  = $M->count();// 查询满足要求的总记录数
        //echo $M->getLastSql();
        $Page   = new \Think\Page($count, C("PAGE_SIZE"));// 实例化分页类 传入总记录数和每页显示的记录数
        $show   = $Page->show();// 分页显示输出
        $list   = $M->limit($Page->firstRow.','.$Page->listRows)->select();
        $result = array('list' => $list, "page" => $show, "count" => $count);
        return $result;
    }
    protected function getModelName() {
        return "DiyForm";
    }

}
